package com.example.enterprise.controller;


import com.example.enterprise.entity.Picture;
import com.example.enterprise.service.PictureService;
import com.example.enterprise.util.CommunityUtil;
import com.example.enterprise.util.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ImageController {

    @Autowired
    private PictureService pictureService;

    @Value("${picture.path.upload}")
    private String uploadPath;

    @Value("${picture.path.domain}")
    private String domain;

    @Value("${server.servlet.context-path}")
    private String contextPath;




//    @RequestMapping(path="upload",method = RequestMethod.POST)
//    public String upLoadImage(MultipartFile image, Model model){
//
//        if(image==null){
//
//        }
//
//        String fileName = image.getOriginalFilename();
//        String suffix = fileName.substring(fileName.lastIndexOf("."));
//        if (StringUtils.isBlank(suffix)) {
//            model.addAttribute("error", "文件的格式不正确!");
//            return "/site/setting";
//        }
//
//        // 生成随机文件名
//        fileName = CommunityUtil.generateUUID() + suffix;
//        // 确定文件存放的路径
//        File dest = new File(uploadPath + "/" + fileName);
//        try {
//            // 存储文件
//            image.transferTo(dest);
//        } catch (IOException e) {
//
//            throw new RuntimeException("上传文件失败,服务器发生异常!", e);
//        }
//
//        // 更新当前用户的头像的路径(web访问路径)
//        // http://localhost:8080/community/user/header/xxx.png
////        User user = hostHolder.getUser();
////        String headerUrl = domain + contextPath + "/user/header/" + fileName;
////        userService.updateHeader(user.getId(), headerUrl);
//
//        return "redirect:/index";
//
//
//    }


    @RequestMapping("/upPic")
    @ResponseBody
    public List<Picture> upPic(@RequestParam("file") MultipartFile multipartFile, HttpSession session) {


        int enterpriseId = 1;
        int picType = 4;
        String picInfo = "大发噶";
        int status = 0;
        int seLogId = 1;

        //添加图片
        String imagePath = FileUploadUtils.upload(multipartFile, session, domain, uploadPath);

        int flag = pictureService.insertPic(picType, enterpriseId, imagePath, picInfo, status);

        //查找所有该enterpriseId并且是安全日志id的
        List<Picture> pictures = pictureService.findPicByAffiliation(seLogId);


        return pictures;
    }

    @RequestMapping("/showImg")
    public void showImg(String path, HttpServletResponse response) {
        FileUploadUtils.showImg(path, response);
    }


    @RequestMapping(path = "/deleteImage/{imageId}", method = RequestMethod.GET)
    private String  delete(@PathVariable("imageId") int imageId, Model model) {

        //picture表将status改变为1
        int flag = pictureService.changeDeleteStatus(imageId);


        return CommunityUtil.getJSONString(0,"删除成功");
    }


}
